#include <observer_pattern.h>
#include <iostream>
#include <bitset>

Subject::~Subject()
{
    vector<Observer*>::iterator it = this->observers.begin();
    for(; it != this->observers.end(); it++)
    {
        delete *it;
    }
}

int Subject::getState()
{
    return this->state;
}

void Subject::setState(int state)
{
    this->state = state;
    this->notifyAllObservers();
}

void Subject::attach(Observer *observer)
{
    this->observers.push_back(observer);
}

void Subject::notifyAllObservers()
{
    vector<Observer*>::iterator it = this->observers.begin();
    for(; it != this->observers.end(); it++)
    {
        (*it)->update();
    }
}

BinaryObserver::BinaryObserver(Subject* subject)
{
    this->subject = subject;
    this->subject->attach(this);
}

BinaryObserver::~BinaryObserver()
{
    delete this->subject;
}

void BinaryObserver::update()
{
    bitset<4> bit(this->subject->getState());
    cout << "Binary String: " << bit << endl;
}

OctalObserver::OctalObserver(Subject* subject)
{
    this->subject = subject;
    this->subject->attach(this);
}

OctalObserver::~OctalObserver()
{
    delete this->subject;
}

void OctalObserver::update()
{
    cout << "Octal String: " << std::oct << this->subject->getState() << endl;
}

HexaObserver::HexaObserver(Subject* subject)
{
    this->subject = subject;
    this->subject->attach(this);
}

HexaObserver::~HexaObserver()
{
    delete this->subject;
}

void HexaObserver::update()
{
    cout << "Hex String: " << std::hex << this->subject->getState() << endl;
}
